%%
%% Actual entry point, mainly spawn T new gossip nodes and one gossip_monitor node, and then initialize
%% all the gossip nodes with information of neighbor information as well as MonitorPid.
%%
%% Wait until received "finish" (success) message from gossip_monitor. 
%%
%% Parameters:
%% 	T: Number of nodes
%% 	Target: Largest possible initial CPU configuration value in the node
%%
%% Returns:
%% 	{Inits, Result}: initial ramdomed CPU configuration value and configuration value after sucessfully completed
%% 	Gossip based resource allocation operation.
%%
run(T, Target) ->
	init_random(),
	IDs = lists:seq(1,T),
	Inits = [random:uniform(Target) ||_ID <- IDs],
	if
		T==1 ->
			Result = [{1,lists:nth(1, Inits),{{0,0,0,0},{0,0}}}],
			{Inits, Result};
		true ->
		    Nodes = lists:map(fun(ID) -> {ID, spawn_link(gossip_node, init, [ID])}
		                     end, IDs),
			
			MonitorPid = spawn_link(gossip_monitor, init, [Nodes, self()]),
			
			[Pid ! {start, self(), MonitorPid, {Nodes, lists:nth(ID, Inits)}}  || {ID, Pid} <- Nodes],
			
			receive
				{finish, Result} ->
							{Inits, Result}
			end
	end.